package com.op.studyingliteapp.activity;

import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;

import android.app.ActionBar;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.op.studyingliteapp.R;
import com.op.studyingliteapp.bean.NoteBean;
import com.op.studyingliteapp.bean.UserBean;
import com.op.studyingliteapp.common.Common;
import com.op.studyingliteapp.database.dao.NoteDao;
import com.op.util.SharedPreferenceUtil;
import com.op.util.UUIDUtil;

public class NoteAddActivity extends Activity {
	private EditText contentEditText;
	private String noteChange;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_note_add);
		
		ActionBar actionBar = getActionBar();
		actionBar.setDisplayHomeAsUpEnabled(true);
		actionBar.setDisplayShowHomeEnabled(false);
		actionBar.setDisplayShowTitleEnabled(true);
		actionBar.setTitle("编写笔记");
		
		SharedPreferences preferences = getSharedPreferences("StudyingLite",
				Context.MODE_PRIVATE);
		noteChange = preferences.getString(Common.Prefs.NOTE_CHANGE, "false");
		contentEditText = (EditText) findViewById(R.id.content);
		if (noteChange.equals("true")) {
			contentEditText.setText(preferences.getString(
					Common.Prefs.NOTE_CONTENT, ""));
		}
		DisplayMetrics dm = new DisplayMetrics();
		// 获取屏幕信息,根据屏幕的高设置输入框的高
		getWindowManager().getDefaultDisplay().getMetrics(dm);
		int screenHeigh = dm.heightPixels;
		contentEditText.setHeight(screenHeigh-280);
	}

	/**
	 * 增加通知按钮监听
	 */
	public void sentNoteClicked(View v) {
		String content = contentEditText.getText().toString();
		if (content.length() > 0) {
			sureSend();
		} else {
			AlertDialog.Builder builder = new Builder(NoteAddActivity.this);
			builder.setMessage("内容不能为空");
			builder.setTitle("提示");
			builder.setNegativeButton("确定", null);
			builder.create();
			builder.show();
		}
	}

	/**
	 * 确认发送通知
	 */
	public void sureSend() {
		AlertDialog.Builder builder = new Builder(NoteAddActivity.this);
		builder.setMessage("确认添加笔记吗？");
		builder.setTitle("提示");
		builder.setNeutralButton("确认", new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				String content = contentEditText.getText().toString();
				SharedPreferences preferences = getSharedPreferences(
						"StudyingLite", Context.MODE_PRIVATE);
				String getUserId = preferences.getString(Common.Prefs.USER_ID,
						"");// 获得用户Id
				String courseId = preferences.getString(Common.Prefs.COURSE_ID,
						"");// 课程Id
				String noteId = preferences.getString(Common.Prefs.NOTE_ID, "");// 笔记Id
				SimpleDateFormat formatter = new SimpleDateFormat(
						"yyyy年MM月dd日    HH:mm:ss     ");
				Date curDate = new Date(System.currentTimeMillis());// 获取当前时间
				String time = formatter.format(curDate);
				NoteBean noteBean = new NoteBean();
				UserBean userBean = new UserBean();
				userBean.setId(getUserId);
				if (noteChange.equals("true")) {
					noteBean.setId(noteId);
					Map<String, Object> prefsMap = new HashMap<String, Object>();
					prefsMap.put(Common.Prefs.NOTE_CHANGE, "false");
					SharedPreferenceUtil.saveData(NoteAddActivity.this,
							prefsMap);
				} else {
					noteBean.setId(UUIDUtil.generate());
				}
				noteBean.setCreateTime(time);
				noteBean.setContent(content);
				noteBean.setCourseId(courseId);
				noteBean.setUserBean(userBean);
				NoteDao noteDao = new NoteDao(NoteAddActivity.this, getUserId);
				noteDao.insertOrUpdate(noteBean);
				finish();
			}
		});
		builder.setNegativeButton("取消", null);
		builder.create();
		builder.show();
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			onBackPressed();
			break;
		default:
			break;
		}
		return super.onOptionsItemSelected(item);
	}
}
